package com.viser.partyinrs.adapters;

import java.util.ArrayList;
import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONObject;

import com.viser.partyinrs.entities.DogadjajKomentar;
import com.viser.partyinrs.http.HttpDispatcher;

public class DogadjajKomentarAdapter extends DataAdapter {
	
	public DogadjajKomentarAdapter() {
		dispatcher = new HttpDispatcher();
	}
	
	@SuppressWarnings("finally")
	public ArrayList<DogadjajKomentar> getDogadjajKomentarEntities(String upit) {
		ArrayList<DogadjajKomentar> dogkomentari = new ArrayList<DogadjajKomentar>();
		try
		{
			String httpResponse = dispatcher.getHttpResponse_GET(pageBase+"dogkomentari_list.php?upit=upit");
			JSONArray dogkomentariArray = new JSONArray(httpResponse);
			for(int i=0; i<dogkomentariArray.length(); i++)
			{
				JSONObject dogkomentar = dogkomentariArray.getJSONObject(i);
				DogadjajKomentar dk = new DogadjajKomentar();
				dk.datumVreme = dogkomentar.getString("datum_vreme");
				dk.idKorisnik = dogkomentar.getInt("id_korisnik");
				dk.idMesto = dogkomentar.getInt("id_mesto");
				dk.redniBroj = dogkomentar.getInt("rb");
				dk.tekst = dogkomentar.getString("tekst");
				dogkomentari.add(dk);
			}
		}
		catch (Exception ex) { }
		finally {
			return dogkomentari;
		}
	}
	
	public boolean dodajDogadjajKomentar(String username, int idDogadjaj, String tekst)
	{
		boolean rezultat=true;
		
		try {
			Vector<String> postParameters = new Vector<String>();
			postParameters.add("username=" + username);
			postParameters.add("id_dogadjaj=" + idDogadjaj);
			postParameters.add("tekst=" + tekst);
						
			String httpResponse = dispatcher.getHttpResponse_POST(pageBase+"dodajdogadjajkomentar.php", postParameters);
			rezultat = httpResponse.trim().equals("1");
		} catch (Exception e) {
			rezultat = false;
		}
		return rezultat;
	}
}
